package im.frape.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

import android.util.Log;

class FrapeRosterListener implements RosterListener {
	private Roster roster;

	public FrapeRosterListener(Roster roster) {
		super();
		this.roster = roster;
	}

	@Override
	public void entriesAdded(Collection<String> arg0) {
		roster.reload();
	}

	@Override
	public void entriesDeleted(Collection<String> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void entriesUpdated(Collection<String> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void presenceChanged(Presence arg0) {
		// TODO Auto-generated method stub

	}

}

public class FrapeRoster {
	public HashMap<String, Roster> rosters;
	public HashMap<String, RosterListener> listeners;
	private static FrapeRoster instance;

	public static FrapeRoster getInstance() {
		if (instance == null) {
			instance = new FrapeRoster();
		}
		return instance;
	}

	public FrapeRoster() {
		this.rosters = new HashMap<String, Roster>();
		this.listeners = new HashMap<String, RosterListener>();
	}

	public HashMap<String, Roster> getRoster() {
		return rosters;
	}

	/*
	 * gets roster associated with given account
	 */
	public Roster getRoster(String account) {
		return this.rosters.get(account);
	}

	/*
	 * adds roster and corresponding listener to roster/listener array
	 */

	public void addRoster(String account, Roster roster) {
		Log.i("FrapeRoster", "Adding account roster for " + account);
		FrapeRosterListener listener = new FrapeRosterListener(roster);
		roster.addRosterListener(listener);
		this.rosters.put(account, roster);
		this.listeners.put(account, listener);
		// roster.reload();
		Log.i("FrapeRoster", "Added account roster for " + account);
	}

	/*
	 * removes given listener and roster
	 */

	public void removeRoster(String account) {
		Log.i("FrapeRoster", "Removing account roster for " + account);
		Roster roster = this.rosters.remove(account);

		// this.rosters.get(account).removeRosterListener(this.listeners.get(account));
		this.rosters.remove(account);
		this.listeners.remove(account);
		Log.i("FrapeRoster", "Removed account roster for " + account);
	}

	public ArrayList<String> getAllNames() {
		Log.i("FrapeRoster", "Get All Names");
		ArrayList<String> names = new ArrayList<String>();
		Collection<Roster> pomRosters = rosters.values();
		for (Roster roster : pomRosters) {
			Log.i("FrapeRoster", Integer.toString(roster.getEntryCount()));
			Collection<RosterEntry> entries = roster.getEntries();
			for (RosterEntry entry : entries) {
				names.add(entry.getName());
			}
		}
		return names;
	}

	public ArrayList<String> getAllUsers() {
		ArrayList<String> users = new ArrayList<String>();
		Collection<Roster> pomRosters = rosters.values();
		for (Roster roster : pomRosters) {
			Log.i("FrapeRoster", Integer.toString(roster.getEntryCount()));
			Collection<RosterEntry> entries = roster.getEntries();
			for (RosterEntry entry : entries) {
				users.add(entry.getUser());
			}
		}
		Collections.sort(users);
		return users;
	}
}
